#include <func.h>

typedef struct {
    int id;
    char name[25];
    char gender;
    int chinese;
    int math;
    int english;
} Student;

void student_display(const Student* s) {
    printf("%d %s %c %d %d %d\n",
           s->id,
           s->name,
           s->gender,
           s->chinese,
           s->math,
           s->english);
}

void print_ids(const char* prefix)
{
    printf("%s: pid=%d, ppid=%d, tid=0x%lx\n",
           prefix,
           getpid(),
           getppid(),
           pthread_self());
}

void* start_routine(void* args) 
{
    print_ids("new thread");
    // Student s = {1, "xixi", 'f', 100, 100, 100}; // 不能在线程的栈上面，线程退出后，对应的栈会被销毁
    Student* s = (Student*)malloc(sizeof(Student));
    s->id = 1;
    strcpy(s->name, "xixi");
    s->gender = 'f';
    s->chinese = 100;
    s->math = 100;
    s->english = 100;

    pthread_exit(s);
}

int main(int argc, char* argv[])
{
    print_ids("main thread");

    pthread_t tid;
    int err = pthread_create(&tid, NULL, start_routine, NULL);
    if (err) {
        fprintf(stderr, "pthread_create(): %s\n", strerror(err));
        exit(1);
    }

    Student* retval;
    pthread_join(tid, (void**)&retval);   
    student_display(retval);

    return 0;  // exit(0)
}

